/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.easemob.chatuidemo.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.easemob.EMCallBack;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMContactManager;
import com.easemob.chat.EMGroupManager;
import com.easemob.chatuidemo.Constant;
import com.easemob.chatuidemo.DemoApplication;
import com.easemob.chatuidemo.DemoHXSDKHelper;
import com.easemob.chatuidemo.R;
import com.easemob.chatuidemo.db.UserDao;
import com.easemob.chatuidemo.domain.User;
import com.easemob.chatuidemo.service.UserService;
import com.easemob.chatuidemo.utils.CommonUtils;
import com.easemob.chatuidemo.utils.StringUtils;
import com.easemob.exceptions.EaseMobException;
import com.easemob.util.EMLog;
import com.easemob.util.HanziToPinyin;

/**
 * 登陆页面
 * 
 */
public class LoginActivity extends BaseActivity {
	public static final int REQUEST_CODE_SETNICK = 1;
	private EditText usernameEditText;
	private EditText passwordEditText;

	private boolean progressShow;
	private boolean autoLogin = false;
	
	private String currentUsername;
	private String currentPassword;
    private ProgressDialog progressDialog;
    private String contactURL;
    
    private List<User> data;
    private LinkedList<User> infos;
    private boolean ISHIDE = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		ISHIDE  = new Intent().getBooleanExtra("ISHIDE", false);
		SharedPreferences perferences = this.getSharedPreferences("filename", Context.MODE_PRIVATE);
		String uid = perferences.getString("uid", "");
		if (uid !=null && !"".equals(uid)) {
            String loginname = perferences.getString("loginname", "");
            String password = perferences.getString("password", "");
            new NetWorkTask().execute(loginname, password);
        }
		// 如果用户名密码都有，直接进入主页面
		if (DemoHXSDKHelper.getInstance().isLogined()) {
			autoLogin = true;
			startActivity(new Intent(LoginActivity.this, MainActivity.class));

			return;
		}
		setContentView(R.layout.activity_login);

		usernameEditText = (EditText) findViewById(R.id.username);
		passwordEditText = (EditText) findViewById(R.id.password);

		// 如果用户名改变，清空密码
		usernameEditText.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				passwordEditText.setText(null);
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {

			}
		});
		if (DemoApplication.getInstance().getUserName() != null) {
			usernameEditText.setText(DemoApplication.getInstance().getUserName());
		}
	}

	/**
	 * 登录
	 * 
	 * @param view
	 */
    public void login(View view) {
        if (!CommonUtils.isNetWorkConnected(this)) {
            Toast.makeText(this, R.string.network_isnot_available,
                    Toast.LENGTH_SHORT).show();
            return;
        }
        currentUsername = usernameEditText.getText().toString().trim();
        currentPassword = passwordEditText.getText().toString().trim();

        if (TextUtils.isEmpty(currentUsername)) {
            Toast.makeText(this, R.string.User_name_cannot_be_empty,
                    Toast.LENGTH_SHORT).show();
            return;
        } 
        if(!StringUtils.isPhoneNo(currentUsername)){
            Toast.makeText(this, "登录帐号必须为手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(currentPassword)) {
            Toast.makeText(this, R.string.Password_cannot_be_empty,
                    Toast.LENGTH_SHORT).show();
            return;
        }
        
        new NetWorkTask().execute(currentUsername,currentPassword);
    }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			if (requestCode == REQUEST_CODE_SETNICK) {
				DemoApplication.currentUserNick = data.getStringExtra("edittext");

				

			}
		}
	}
	
	private void login(){
	    progressShow = true;
        final ProgressDialog pd = new ProgressDialog(LoginActivity.this);
        pd.setCanceledOnTouchOutside(false);
        pd.setOnCancelListener(new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                progressShow = false;
            }
        });
        pd.setMessage(getString(R.string.Is_landing));
        pd.show();

        final long start = System.currentTimeMillis();
        // 调用sdk登陆方法登陆聊天服务器
//      new UserService().Login(currentUsername, currentPassword);
        EMChatManager.getInstance().login(currentUsername, currentPassword, new EMCallBack() {

            @Override
            public void onSuccess() {
                if (!progressShow) {
                    return;
                }
                // 登陆成功，保存用户名密码
                DemoApplication.getInstance().setUserName(currentUsername);
                DemoApplication.getInstance().setPassword(currentPassword);
                
                runOnUiThread(new Runnable() {
                    public void run() {
                        pd.setMessage(getString(R.string.list_is_for));
                    }
                });
                try {
                    // ** 第一次登录或者之前logout后再登录，加载所有本地群和回话
                    // ** manually load all local groups and
                    // conversations in case we are auto login
                    EMGroupManager.getInstance().loadAllGroups();
                    EMChatManager.getInstance().loadAllConversations();
                    //处理好友和群组
                    processContactsAndGroups();
                } catch (Exception e) {
                    e.printStackTrace();
                    //取好友或者群聊失败，不让进入主页面
                    runOnUiThread(new Runnable() {
                        public void run() {
                            pd.dismiss();
                            DemoApplication.getInstance().logout(null);
                            Toast.makeText(getApplicationContext(), R.string.login_failure_failed, 1).show();
                        }
                    });
                    return;
                }
                //更新当前用户的nickname 此方法的作用是在ios离线推送时能够显示用户nick
                boolean updatenick = EMChatManager.getInstance().updateCurrentUserNick(DemoApplication.currentUserNick.trim());
                if (!updatenick) {
                    Log.e("LoginActivity", "update current user nick fail");
                }
                if (!LoginActivity.this.isFinishing())
                    pd.dismiss();
                // 进入主页面
                startActivity(new Intent(LoginActivity.this, MainActivity.class).putExtra("ISHIDE", ISHIDE));
                finish();
            }

           

            @Override
            public void onProgress(int progress, String status) {
            }

            @Override
            public void onError(final int code, final String message) {
                if (!progressShow) {
                    return;
                }
                runOnUiThread(new Runnable() {
                    public void run() {
                        pd.dismiss();
                        Toast.makeText(getApplicationContext(), getString(R.string.Login_failed) + message, Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
	}

	 private void processContactsAndGroups() throws EaseMobException {
         // demo中简单的处理成每次登陆都去获取好友username，开发者自己根据情况而定
         List<String> usernames = EMContactManager.getInstance().getContactUserNames();
         String sendTels = StringUtils.listToString(usernames);
         contactURL = "http://www.365jiaju.com/app/im/getContactsInfo.ashx?loginnames=" + sendTels;
         if (sendTels != null && !"".equals(sendTels)) {
             new getContactInfoNetWork().execute(sendTels);
        }else{
            Map<String, User> userlist = new HashMap<String, User>();
            for (String username : usernames) {
                User user = new User();
                user.setUsername(username);
                setUserHearder(username, user);
                userlist.put(username, user);
          }
            // 添加user"申请与通知"
            User newFriends = new User();
            newFriends.setUsername(Constant.NEW_FRIENDS_USERNAME);
            String strChat = getResources().getString(R.string.Application_and_notify);
            newFriends.setNick(strChat);
            
            userlist.put(Constant.NEW_FRIENDS_USERNAME, newFriends);
            // 添加"群聊"
            User groupUser = new User();
            String strGroup = getResources().getString(R.string.group_chat);
            groupUser.setUsername(Constant.GROUP_USERNAME);
            groupUser.setNick(strGroup);
            groupUser.setHeader("");
            userlist.put(Constant.GROUP_USERNAME, groupUser);

            // 存入内存
            DemoApplication.getInstance().setContactList(userlist);
            System.out.println("----------------"+userlist.values().toString());
            // 存入db
            UserDao dao = new UserDao(LoginActivity.this);
            List<User> users = new ArrayList<User>(userlist.values());
            dao.saveContactList(users);
            
            //获取黑名单列表
            List<String> blackList;
            try {
                blackList = EMContactManager.getInstance().getBlackListUsernamesFromServer();
              //保存黑名单
                EMContactManager.getInstance().saveBlackList(blackList);

                // 获取群聊列表(群聊里只有groupid和groupname等简单信息，不包含members),sdk会把群组存入到内存和db中
                EMGroupManager.getInstance().getGroupsFromServer();
            } catch (EaseMobException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
//         EMLog.d("roster", "contacts size: " + usernames.size());
//         Map<String, User> userlist = new HashMap<String, User>();
//         for (String username : usernames) {
//             User user = new User();
//             user.setUsername(username);
//             setUserHearder(username, user);
//             userlist.put(username, user);
//         }
         
     }
	
	/**
	 * 注册
	 * 
	 * @param view
	 */
	public void register(View view) {
		startActivityForResult(new Intent(this, RegisterActivity.class), 0);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (autoLogin) {
			return;
		}
	}
	
	/**
	 * 设置hearder属性，方便通讯中对联系人按header分类显示，以及通过右侧ABCD...字母栏快速定位联系人
	 * 
	 * @param username
	 * @param user
	 */
	protected void setUserHearder(String username, User user) {
		String headerName = null;
		if (!TextUtils.isEmpty(user.getNickname())) {
			headerName = user.getNickname();
		} else {
			headerName = user.getUsername();
		}
		if (username.equals(Constant.NEW_FRIENDS_USERNAME)) {
			user.setHeader("");
		} else if (Character.isDigit(headerName.charAt(0))) {
			user.setHeader("#");
		} else {
			user.setHeader(HanziToPinyin.getInstance().get(headerName.substring(0, 1)).get(0).target.substring(0, 1).toUpperCase());
			char header = user.getHeader().toLowerCase().charAt(0);
			if (header < 'a' || header > 'z') {
				user.setHeader("#");
			}
		}
	}
	
	
	void progressDialogInit(){
        progressDialog = new ProgressDialog(this);
        progressDialog.setIcon(android.R.drawable.ic_menu_info_details);
        progressDialog.setTitle("正在登录");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
        progressDialog.setMessage("请稍后...");
    }
	
	class NetWorkTask extends AsyncTask<String, Integer, String>{
        @Override
        protected void onPreExecute() {
            progressDialogInit();
            progressDialog.show();
            super.onPreExecute();
        }
        
        @Override
        protected String doInBackground(String... params) {
            UserService uService = new UserService();
            String retCode = uService.Login(getApplicationContext(),params[0], params[1]);
            return retCode;
        }


        @Override
        protected void onPostExecute(String result) {
            if (result == null) {
                progressDialog.dismiss();
                AlertDialog.Builder builder = new Builder(LoginActivity.this);
                builder.setIcon(android.R.drawable.ic_dialog_info);
                builder.setMessage("网络连接失败");
                builder.setTitle("提示");
                builder.setPositiveButton("确认", new OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
                return;
            }
            if ("0000".equals(result)) {// 成功登录
                progressDialog.dismiss();
                login();
//                Intent intent = new Intent(LoginActivity.this,
//                        com.easemob.chatuidemo.activity.AlertDialog.class);
//                intent.putExtra("editTextShow", true);
//                intent.putExtra("titleIsCancel", true);
//                intent.putExtra("msg",getResources().getString(R.string.please_set_the_current));
//                intent.putExtra("edit_text", currentUsername);
//                startActivityForResult(intent, REQUEST_CODE_SETNICK);
//                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
            } else {
                AlertDialog.Builder builder = new Builder(LoginActivity.this);
                builder.setIcon(android.R.drawable.ic_dialog_info);
                if ("101".equals(result)) {
                    builder.setMessage("返回结果：" + "帐号或密码错误，请重新输入");
                }else{
                    builder.setMessage("返回结果：" + result + "未知错误");
                }
                
                builder.setTitle("提示");
                builder.setPositiveButton("确认", new OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
            }
            progressDialog.dismiss();
            super.onPostExecute(result);
        }
        
    }
	
	class getContactInfoNetWork extends AsyncTask<String, Integer, List<User>>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
        
        @Override
        protected List<User> doInBackground(String... params) {
            UserService userService = new UserService();
            data = userService.getConectsInfos(params[0]);
            return data;
        }

        @Override
        protected void onPostExecute(List<User> result) {
//            infos = new LinkedList<User>();
            if (null == result) {
                return;
            }
            if(result.size() > 0 && null != result){
                Map<String, User> userlist = new HashMap<String, User>();
                for (User user : result) {
                    user.setUsername(user.getLoginname());
                    setUserHearder(user.getNickname(), user);
                    userlist.put(user.getLoginname(), user);
                }
                // 添加user"申请与通知"
                User newFriends = new User();
                newFriends.setUsername(Constant.NEW_FRIENDS_USERNAME);
                String strChat = getResources().getString(R.string.Application_and_notify);
                newFriends.setNick(strChat);
                
                userlist.put(Constant.NEW_FRIENDS_USERNAME, newFriends);
                // 添加"群聊"
                User groupUser = new User();
                String strGroup = getResources().getString(R.string.group_chat);
                groupUser.setUsername(Constant.GROUP_USERNAME);
                groupUser.setNick(strGroup);
                groupUser.setHeader("");
                userlist.put(Constant.GROUP_USERNAME, groupUser);

                // 存入内存
                DemoApplication.getInstance().setContactList(userlist);
                System.out.println("----------------"+userlist.values().toString());
                // 存入db
                UserDao dao = new UserDao(LoginActivity.this);
                List<User> users = new ArrayList<User>(userlist.values());
                dao.saveContactList(users);
                
                //获取黑名单列表
                List<String> blackList;
                try {
                    blackList = EMContactManager.getInstance().getBlackListUsernamesFromServer();
                  //保存黑名单
                    EMContactManager.getInstance().saveBlackList(blackList);

                    // 获取群聊列表(群聊里只有groupid和groupname等简单信息，不包含members),sdk会把群组存入到内存和db中
                    EMGroupManager.getInstance().getGroupsFromServer();
                } catch (EaseMobException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                

            }
            super.onPostExecute(result);
        }

        
        
    }
	
}
